/**
 *  使用桌面通知
 */
define(function(require, exports, module) {
  "require:nomunge,exports:nomunge,module:nomunge";
  // 桌面通知所有实例
  if (!window._cache['notiList']) {
    window._cache['notiList'] = {};
  }

  /**
   * 创建基本的桌面通知（文本）
   */
  exports.noti = function(id, title, content, cover){
    if ( !id ) return;
    if(window.webkitNotifications) {
      var noti = webkitNotifications.createNotification(
          (cover ? cover : '/images/icon-48.png'),
          title,
          content
        );
      noti.replaceId = id;
      noti.show();

      window._cache['notiList'][id] = noti;
      return noti;
    }
    
    // 使用chrome新的api 2014-05-22
    chrome.notifications.clear(id, function(result){
      var opts = {
          type: "basic", 
          title: title, 
          message: content, 
          iconUrl: cover ? cover : '/images/icon-48.png'
      };
      chrome.notifications.create(id, opts,function(id){
        window._cache['notiList'][id] = true;
      });
    });
    
    return true;
  };

  /**
   * 创建带html的桌面通知
   */
  exports.notiHtml = function(id, file){
    var noti = webkitNotifications.createHTMLNotification(
        file
      );
    noti.replaceId = id;
    noti.show();

    window._cache['notiList'][id] = noti;
    return noti;
  };

  /**
   * 隐藏提示信息
   */
  exports.close = function(id){
    
    if(!window.webkitNotifications) {
      chrome.notifications.clear(id, function(result){
        window._cache['notiList'][id] = false;
      });
      return true;
    }
    
    if ( window._cache['notiList'][id] ) {
      if(typeof window._cache['notiList'][id].cancel == 'function') {
        window._cache['notiList'][id].cancel();
      } else if (typeof window._cache['notiList'][id].close == 'function') {
        window._cache['notiList'][id].close();
      }
    }
  };
});